package com.xueyu.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.domain.Orderdetail;
import com.xueyu.domain.goods.Orders;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface OrdersDao extends BaseMapper<Orders> {

    @Select("SELECT od.*, d.*,  products.productname,supplier.supplierName FROM orders od  JOIN product d ON od.prodId = d.prodId JOIN products ON d.style = products.productid JOIN supplier ON d.supplierId = supplier.supplierId  WHERE od.orderNo IN (${orderNos})")
    @Results({
            @Result(property = "orderdetailid", column = "orderDetailId"),//订单id
            @Result(property = "orderno", column = "orderNo"),//订单号
            @Result(property = "sytle", column = "sytle"),//状态
            @Result(property = "prodid", column = "prodId"),//商品ID（关联product表主键）
            @Result(property = "unitname", column = "unitName"),//单位
            @Result(property = "price", column = "price"),//价格
            @Result(property = "prodcount", column = "prodCount"),//数量
            @Result(property = "totalprice", column = "totalPrice"),//小记
            @Result(property = "time", column = "time"),//小记

            @Result(property = "product.prodcount", column = "prodCount"),//数量
            @Result(property = "product.prodid", column = "prodId"),
            @Result(property = "product.prodname", column = "prodName"),
            @Result(property = "product.unitname", column = "unitName"),
            @Result(property = "product.brand", column = "brand"),
            @Result(property = "product.style", column = "productname"),
            @Result(property = "product.price", column ="price"),
            @Result(property = "product.supplierid", column = "supplierName"),
            @Result(property = "product.ctime", column = "ctime"),
            @Result(property = "product.Gtime", column = "Gtime"),
            @Result(property = "product.DrugM", column = "DrugM"),
    })
    List<Orders> selectOrderDetailWithDrugsByOrderNoList(String orderNos);


    @Select("SELECT od.*, d.*,  products.productname,supplier.supplierName FROM  orderdetail od  JOIN product d ON od.prodId = d.prodId JOIN products ON d.style = products.productid JOIN supplier ON d.supplierId = supplier.supplierId  WHERE od.orderNo IN (${orderNos})")
    @Results({
            @Result(property = "orderno", column = "orderNo"),//订单号
            @Result(property = "prodid", column = "prodId"),//状态
            @Result(property = "orderDate", column = "orderDate"),//单位
            @Result(property = "personId", column = "personId"),//价格
            @Result(property = "bz", column = "bz"),//数量
            @Result(property = "c", column = "c"),//小记

            @Result(property = "product.prodcount", column = "c"),//数量
            @Result(property = "product.prodid", column = "prodId"),
            @Result(property = "product.prodname", column = "prodName"),
            @Result(property = "product.unitname", column = "unitName"),
            @Result(property = "product.brand", column = "brand"),
            @Result(property = "product.style", column = "productname"),
            @Result(property = "product.price", column ="price"),
            @Result(property = "product.supplierid", column = "supplierName"),
            @Result(property = "product.ctime", column = "ctime"),
            @Result(property = "product.Gtime", column = "Gtime"),
            @Result(property = "product.DrugM", column = "DrugM"),
    })
    List<Orderdetail> selectOrderDetailWithDrugsByOrderNoList2(String orderNos);



    @Select("<script>" +
            "SELECT DISTINCT orderNo " +
            "FROM orders " +
            "WHERE 1=1 " +
            "<if test='orderno != null and orderno != \"\"'>" +
            "AND orderNo = #{orderno} " +
            "</if>"
            +
            "<if test='style != null and style != \"\"'>" +
            "AND sytle = #{style} " +
            "</if>"
            +
            "</script>")

    List<String> getOrderNoList(Page page, @Param("orderno") String orderno, @Param("style") String style);

    @Select("<script>" +
            "SELECT DISTINCT orderNo " +
            "FROM orderdetail " +
            "WHERE 1=1 " +
            "<if test='orderno != null and orderno != \"\"'>" +
            "AND orderNo = #{orderno} " +
            "</if>"
            +
            "<if test='style != null and style != \"\"'>" +
            "AND personId = #{style} " +
            "</if>"
            +
            "</script>")
    List<String> getOrderNoList2(Page page, @Param("orderno") String orderno, @Param("style") String style);





    @Update("<script>" +
            "UPDATE orders " +
            "SET sytle = 1 " +
            "WHERE 1=1 " +
            "<if test='orderNo != null and orderNo != \"\"'>" +
            "AND orderNo = #{orderNo} " +
            "</if>"
            +
            "</script>")
    int updateOrderNo(@Param("orderNo") String orderNo);


    @Delete("<script>" +
            "DELETE FROM orders " +
            "WHERE 1=1 " +
            "<if test='orderNo != null and orderNo != \"\"'>" +
            "AND orderNo = #{orderNo} " +
            "</if>" +
            "</script>")
    int deleteByOrderNo(@Param("orderNo") String orderNo);

    @Delete("<script>" +
            "DELETE FROM orderdetail " +
            "WHERE 1=1 " +
            "<if test='orderNo != null and orderNo != \"\"'>" +
            "AND orderNo = #{orderNo} " +
            "</if>" +
            "</script>")
    int deleteByOrderNo2(@Param("orderNo") String orderNo);
}
